package com.kds.smarthome.controller;

import com.kds.smarthome.common.AjaxResponse;
import com.kds.smarthome.common.EnergyType;
import com.kds.smarthome.entity.Building;
import com.kds.smarthome.service.BuildingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/api/buildings")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;


    @GetMapping("/user")
    public AjaxResponse<List<Building>> getBuildingsByUser(@RequestParam String username) {

        List<Building> buildings =  buildingService.findByUsername(username);

        return AjaxResponse.success(buildings);
    }

    @GetMapping
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<List<Building>> getBuildingsByAdmin() {

        List<Building> buildings =buildingService.findAll();
        return AjaxResponse.success(buildings);
    }

    @GetMapping("/{tid}")
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<Building> getDetail(@PathVariable Integer tid) {
        Building building = buildingService.findById(tid);
        return AjaxResponse.success(building);
    }

    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> create(@RequestBody Building building) {
        buildingService.createBuilding(building);
        return AjaxResponse.success("创建建筑成功");
    }

    @PutMapping("/{tid}")
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> update(@PathVariable Integer tid, @RequestBody Building building) {
        building.setTid(tid);
        // 获取受影响的用户列表
        List<String> affectedUsers = buildingService.findUsersByBuildingCode(building.getCode());

        buildingService.updateBuilding(building);


        return AjaxResponse.success("更新建筑成功");
    }

    @DeleteMapping("/{tid}")
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> delete(@PathVariable Integer tid) {
        buildingService.deleteBuilding(tid);
        return AjaxResponse.success("删除建筑成功");
    }

    @PostMapping("/assign")
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> assignToUser(@RequestParam String username, @RequestParam String buildingCode) {
        buildingService.assignBuildingToUser(username, buildingCode);

        return AjaxResponse.success("分配建筑成功");
    }

    @DeleteMapping("/assign")
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> removeFromUser(@RequestParam String username, @RequestParam String buildingCode) {
        buildingService.removeBuildingFromUser(username, buildingCode);

        return AjaxResponse.success("移除建筑成功");
    }

    /**
     * 获取建筑的能源类型列表
     */
    @GetMapping("/{code}/energy-types")
    public AjaxResponse<List<EnergyType>> getBuildingEnergyTypes(@PathVariable String code) {
        List<EnergyType> energyTypes = buildingService.getBuildingEnergyTypes(code);
        return AjaxResponse.success(energyTypes);
    }

    /**
     * 设置建筑的能源类型
     */
    @PutMapping("/{code}/energy-types")
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> setBuildingEnergyTypes(
            @PathVariable String code,
            @RequestBody Map<String, Set<EnergyType>> payload) {

        // 获取受影响的用户列表
        List<String> affectedUsers = buildingService.findUsersByBuildingCode(code);

        // 设置能源类型
        buildingService.setBuildingEnergyTypes(code, payload.get("energyTypes"));



        return AjaxResponse.success("设置建筑能源类型成功");
    }

    /**
     * 添加建筑能源类型
     */
    @PostMapping("/{code}/energy-types")
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> addBuildingEnergyType(
            @PathVariable String code,
            @RequestBody Map<String, String> payload) {

        EnergyType energyType = EnergyType.fromCode(payload.get("energyType"));

        // 获取受影响的用户列表
        List<String> affectedUsers = buildingService.findUsersByBuildingCode(code);

        // 添加能源类型
        buildingService.addBuildingEnergyType(code, energyType);

        return AjaxResponse.success("添加建筑能源类型成功");
    }

    /**
     * 删除建筑能源类型
     */
    @DeleteMapping("/{code}/energy-types/{energyType}")
    @PreAuthorize("hasRole('ADMIN')")
    public AjaxResponse<?> removeBuildingEnergyType(
            @PathVariable String code,
            @PathVariable String energyType) {

        EnergyType type = EnergyType.fromCode(energyType);

        // 获取受影响的用户列表
        List<String> affectedUsers = buildingService.findUsersByBuildingCode(code);

        // 删除能源类型
        buildingService.removeBuildingEnergyType(code, type);



        return AjaxResponse.success("删除建筑能源类型成功");
    }
}
